package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.BitCard;
import com.qiyeji.real_estate.entity.BitCardKey;
import com.qiyeji.real_estate.entity.timeTable;
import com.qiyeji.real_estate.entity.timeTableKey;
import com.qiyeji.real_estate.service.BitCardService;
import com.qiyeji.real_estate.service.TimeTableService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/BitCard")
@RestController
public class BitCardController {

    @Autowired
    BitCardService bitCardService;

    @Autowired
    TimeTableService timeTableService;

    @PostMapping("/GetAll")
    public Result getAll() {
        List<BitCard> bitCards = bitCardService.getAllBitCard();

        if (bitCards == null)
            return Result.noRecord();
        else
            return Result.success(bitCards);
    }

    @PostMapping("/Insert")
    public Result insert(@RequestBody Map<String, String> params) throws ParseException {
        BitCard bitCard = new BitCard(
                params.get("dCode"),
                params.get("sCode"),
                params.get("sName"),
                params.get("starttime"));

        timeTable temp = timeTableService.selectForCheck(new timeTableKey(params.get("starttime"), params.get("sCode")));
        if (temp == null){
            Integer res = bitCardService.insert(bitCard);

            if (res == 0)
                return Result.error();
            else
                return Result.success(res);
        }
        else
            return Result.error("你已打卡，无需重复！");

    }

    @PostMapping("/GetBitCardByStartTime")
    public Result getBitCardByStartTime(@RequestBody Map<String, String> params) {
        List<BitCard> bitCards = bitCardService.getBitCardBySartTime(params.get("startTime"));

        if (bitCards == null)
            return Result.noRecord();
        else
            return Result.success(bitCards);
    }

    @PostMapping("/Update")
    public Result update(@RequestBody Map<String, String> params) {
        Integer res = bitCardService.update(new BitCard(
                params.get("dCode"),
                params.get("sCode"),
                params.get("sName"),
                params.get("starttime"),
                params.get("endtime"))
        );

        if (res == 0)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/GetAllDataBySCode")
    public Result getAllDataBySCode(@RequestBody Map<String, String> params) {
        List<BitCard> res = bitCardService.getAllDataBySCode(params.get("sCode"));

        if (res == null)
            return Result.error("无任何记录！");
        else
            return Result.success(res);
    }
}
